﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Nantianmen.Domain.Services.Destination;

namespace Nantianmen.Endpoints.Destinations
{
    public class DestinationManagementEndpoint : IEndpoint
    {
        public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder builder)
        {
            var routeGroup = builder.MapGroup("/destinations");

            routeGroup.MapGet("/all", async (IDestinationService destinationService) =>
            {
                return await destinationService.ListAllAsync();
            }).WithName("Destination_ListAll");

            routeGroup.MapGet("/id/{id}", async ([FromQuery] string id, IDestinationQueryService service) =>
            {
                var dto = await service.FindByIdAsync(id);
                return dto;
            }).WithName("Destination_GetById");

            routeGroup.MapGet("/address/{address}", async ([FromQuery] string address, IDestinationQueryService service) =>
            {
                var dtos = await service.FindByAddressAsync(address);
                return dtos;
            }).WithName("Destination_GetByAddress");

            routeGroup.MapPost("/create", async ([FromBody] SaveDestinationRequest request, IDestinationService service) =>
            {
                await service.CreateAsync(request);
            }).WithName("Destination_Create");

            routeGroup.MapPost("/update", async ([FromBody] SaveDestinationRequest request, IDestinationService service) =>
            {
                await service.UpdateAsync(request);
            }).WithName("Destination_Update");

            routeGroup.MapPost("/remove", async ([FromBody] RemoveDestinationRequest request, IDestinationService service) =>
            {
                await service.RemoveAsync(request);
            }).WithName("Destination_Remove");

            routeGroup.WithParameterValidation().WithTags("Destinations");

            return builder;
        }
    }
}
